package util::im;

use strict;
use LWP;

sub new {
    my $class = shift;
    my $param = shift;
    my $self = {};
    $self->{'user'} = $param->{'user'};
    $self->{'from'} = $param->{'from'};
    $self->{'url'} = $param->{'api'};
    bless $self,$class;
    return $self;
}

sub message
{
    my $self = shift;
    @_  ?   $self->{'msg'} = shift
        :   $self->{'msg'};
}

sub api
{
    my $self = shift;
    @_  ?   $self->{'url'} = shift
        :   $self->{'url'};
}

sub sendmsg
{
    my $self = shift;

    return "please set im api" unless $self->{'url'};

    my $info = {
        'user'      => $self->{'user'},
        'message'   => $self->{'msg'},
        'from'      => $self->{'from'},
    };
    my $browser = LWP::UserAgent->new( );
    $browser->timeout('10');
    my $resp = $browser->post($self->{'url'}, $info);
    return ($resp->content, $resp->status_line, $resp->is_success, $resp)
    if wantarray;
}

1;
